package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/mMailWF/forShow")
public class MMailWFforShowController extends BaseController {
    private String prefix = "system/mMailWF/forShow";
    @Autowired
    private IMwfJudgeService mwfJudgeService;
    @Autowired
    private IMmailWorkLogService mmailWorkLogService;
    @Autowired
    private MmailWorkFormService mmailWorkFormService;
    @Autowired
    private MwfSendBackService mwfSendBackService;
    @Autowired
    private IMailGetDealService mailGetDealService;

    /**
     * 工单id对应回访记录 页面
     *
     * @param wfId
     * @param map
     * @return
     */
    @GetMapping("/judgeList/{wfId}")
    public ModelAndView judgeList(@PathVariable Integer wfId, ModelMap map) {
        map.put("wfId", wfId);
        return new ModelAndView(prefix + "/judgeList");
    }

    /**
     * 工单id对应回访记录 列表数据
     *
     * @param wfId
     * @return
     */
    @PostMapping("judgeListJson/{wfId}")
    public TableDataInfo judgeListJson(@PathVariable Integer wfId) {
        startPage();
        List<Map<String, Object>> list = mwfJudgeService.judgeListJson(wfId);
        return getDataTable(list);
    }

    /**
     * 回访记录详情页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @GetMapping("/judgeDetail/{pkId}")
    public ModelAndView judgeDetail(@PathVariable Integer pkId, ModelMap map) {
        MwfJudgeEty mwfJudgeEty = mwfJudgeService.getById(pkId);
        map.put("mwfJudge", mwfJudgeEty);
        return new ModelAndView(prefix + "/judgeDetail");
    }

    /**
     * 工单id对应操作记录 页面
     *
     * @param wfId
     * @param map
     * @return
     */
    @GetMapping("/logList/{wfId}")
    public ModelAndView logList(@PathVariable Integer wfId, ModelMap map) {
        map.put("wfId", wfId);
        return new ModelAndView(prefix + "/logList");
    }

    /**
     * 工单id对应回访记录 列表数据
     *
     * @param wfId
     * @return
     */
    @PostMapping("logListJson/{wfId}")
    public TableDataInfo logListJson(@PathVariable Integer wfId) {
        startPage();
        List<Map<String, Object>> list = mmailWorkLogService.logListJson(wfId);
        return getDataTable(list);
    }

    /**
     * 进入派单办理情况详情页面
     * @param sbId 派单id
     * @param map 模型对象
     * @return 视图
     */
    @GetMapping("/dealDetail/{id}")
    public ModelAndView dealDetail(@PathVariable("id") Integer sbId, ModelMap map) {
        map.put("sendBackEty",mwfSendBackService.getById(sbId));
        return new ModelAndView(prefix+"/dealDetail");
    }

    /**
     * 派单情况记录列表页面
     *
     * @param wfId 工单编号
     * @param map 模型对象
     * @return 视图对象
     */
    @GetMapping("/dealList/{wfId}")
    public ModelAndView dealList(@PathVariable("wfId") Integer wfId,Integer sbId,ModelMap map) {
        map.put("wfId",wfId);
        map.put("sbId",sbId);
        map.put("deptName",getSysUser().getDeptName());
        return new ModelAndView(prefix+"/dealList");
    }

    /**
     * 派单情况列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/dealListJson/{wfId}")
    public TableDataInfo dealListJson(@PathVariable("wfId") Integer wfId) {
        startPage();
        List<MwfSendBack> list = mwfSendBackService.dealListJson(wfId);
        return getDataTable(list);
    }

    /**
     * 信件工单 详情
     */
    @GetMapping("/mailDetail/{pkId}")
    public ModelAndView mailDetail(@PathVariable Integer pkId,ModelMap map) {
        MmailWorkForm mmailWorkForm = mmailWorkFormService.getById(pkId);
        map.put("mmailWorkForm",mmailWorkForm);
        MmailGetDealEty mmailGetDealEty = mailGetDealService.getById(mmailWorkForm.getOriginFK());
        map.put("mailGetDeal",mmailGetDealEty);
        return new ModelAndView(prefix+"/mailDetail");
    }


    /**
     * 检查二级部门下派的派单记录是否有下级回复
     * @param wfId 工单id
     * @param sdeptId 拟派部门编号
     * @return
     */
    @PostMapping("/checkSendBack")
    public Map<String,String> checkSendBack(Integer wfId,String sdeptId){
        String backDesc = "已回复";
        List<CheckSendBack> sendBack = mwfSendBackService.checkSendBack(wfId,sdeptId);

        for (CheckSendBack str : sendBack) {
            if ((str.getBackDesc() == null) || ("".equals(str.getBackDesc()))) {
                backDesc = "";
                break;
            }
        }
        if (sdeptId.length() > 6) {
            backDesc = "";
        }

        Map<String,String> result = new HashMap<>();
        result.put("backDesc", backDesc);
        return result;
    }


    /**
     * 进入历史处理记录详情页面
     * @param logId 日志id
     * @param map 模型对象
     * @return 视图
     */
    @GetMapping("/logDetail/{logId}")
    public ModelAndView logDetail(@PathVariable("logId") Integer logId, ModelMap map) {
        map.put("logEty",mmailWorkLogService.getById(logId));
        return new ModelAndView(prefix+"/logDetail");
    }

    /**
     * 日志切换公开状态
     *
     * @return
     */
    @PostMapping("/updateState/{logId}")
    public AjaxResult updateState(@PathVariable("logId")Integer logId) {
        return toAjax(mmailWorkLogService.updateState(logId));
    }

    /**
     * 打印转办单
     * @param ids 派单编号
     * @return 页面
     */
    @GetMapping("/printSends")
    public ModelAndView printSends(String ids){
        ModelAndView mav = new ModelAndView(prefix+"/printSends");
        String[] idList = ids.split(",");
        List<MwfSendBack> list = new ArrayList<>();
        for (String id : idList) {
            MwfSendBack send = mwfSendBackService.getById(Integer.parseInt(id));
            send.setMworkForm(mmailWorkFormService.getById(send.getWfId()));
            MmailGetDealEty mailRecord = mailGetDealService.getById(send.getMailPk());
            // 设置信件渠道来源
            send.setFkstring(mailRecord.getSpareString());
            list.add(send);
        }
        mav.addObject("sends",list);
        return mav;
    }
}
